﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HtmlAgilityPack;
using CORE;

namespace KM.Areas.ADMIN.Controllers
{
    public class HPHtmlWebController : Controller
    {
        //
        // GET: /ADMIN/HPHtmlWeb/

        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult GetCategories(FormCollection collection)
        {
            string url = collection["Url"].ToString();
            string nodeTieuDe = "div/ul/li/span/a";
            HtmlWeb htmlWeb = new HtmlWeb();
            // Creates an HtmlDocument object from an URL
            HtmlAgilityPack.HtmlDocument document = htmlWeb.Load(url);
            var nodes = from foo in document.DocumentNode.Descendants("div")
                        where foo.Attributes["class"] != null && foo.Attributes["class"].Value.Contains("left_menu")
                        select foo;
            int i = 2;
            string a = "";
            if (nodes != null)
            {
                foreach (HtmlAgilityPack.HtmlNode item in nodes)
                {
                    var nodes2 = item.SelectNodes(nodeTieuDe);
                    //a += TieuDe.InnerText;
                    foreach (var item2 in nodes2)
                    {
                        a = string.Format("Insert into LoaiSanPham(TenNhomSanPham, IdCha, ThuTu)values(N'{0}',0,{1})", item2.InnerText.Remove(0, 2), i++);
                        FileTool.Log(a);
                    }

                }

            }
            ViewBag.me = a;
            return View();

        }

        int k = 0;
        public ActionResult GetProduct(FormCollection collection)
        {
            //http://remthinhphat.com/rem-vai.html?page=1

            string url = collection["Url"].ToString();
            string nodeName = "a[@class=\"tensp\"]";
            string nodeImage = "a/img";
            HtmlWeb htmlWeb = new HtmlWeb();
            // Creates an HtmlDocument object from an URL
            HtmlAgilityPack.HtmlDocument document = htmlWeb.Load(url);
            var nodes = from foo in document.DocumentNode.Descendants("div")
                        where foo.Attributes["class"] != null && foo.Attributes["class"].Value.Contains("sp")
                        select foo;
            int i = 2;
            string a = "";
            if (nodes != null)
            {
                foreach (HtmlAgilityPack.HtmlNode item in nodes)
                {
                    HtmlNode img = item.SelectSingleNode(nodeImage);
                    string filename = DateTime.Now.ToString("ddmmyyhhmmss").ToString() + k++.ToString() + ".jpg";
                    if (img != null)
                    {
                        Utils.DownloadImageFromUrl(img.Attributes["src"].Value, filename);
                        
                        HtmlNode Name = item.SelectSingleNode(nodeName);
                        if (Name != null)
                            //a += Name.InnerText;
                            a = string.Format("insert into SanPham(TenSanPham,Image,LoaiSanPhamId) values(N'{0}','{1}',{2})",Name.InnerText, filename, 1);
                    }
                    FileTool.Log(a);


                }

            }
            ViewBag.me = a;
            return View();

        }

    }
}
